package org.example.redis.config;

import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@Slf4j
public class RedissonConfig {
    @Value("${spring.redis.host}")
    private String host;
    @Value("${spring.redis.password}")
    private String password;

    // 移除成员变量 redisAddr，改为在 @Bean 方法中动态拼接
    @Bean
    public RedissonClient redissonClient() {
        Config config = new Config();
        // 直接在配置时拼接地址（此时 host 已被注入）
        String redisAddr = "redis://" + host + ":6379";
        config.useSingleServer()
                .setAddress(redisAddr)
                .setPassword(password);
        log.info("注入的 Redis host: {}", host);
        log.info("注入的 Redis password: {}", password);
        return Redisson.create(config);
    }
}